package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author fanYuLe
 * @Date 2023/3/5 17:01
 * @Blog blog.fanYuLe.net/996.mba
 * @Description:
 */
@RestController
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    //分页查询套餐
    @GetMapping("/setmeal/page")
    public ResultInfo findByPage(@RequestParam(value = "page",defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                 String name){
        Page<Setmeal> page=setmealService.findByPage(pageNum,pageSize,name);
        return ResultInfo.success(page);
    }
    //新增套餐
    @CacheEvict(value = "setmeal",key = "#setmeal.categoryId")
    @PostMapping("/setmeal")
    public ResultInfo save(@RequestBody Setmeal setmeal){
        setmealService.save(setmeal);
        return ResultInfo.success();
    }
    //修改套餐
    //回显
    @GetMapping("/setmeal/{setId}")
    public ResultInfo updateSetmeal(@PathVariable("setId") Long setId){
        Setmeal setmeal=setmealService.findById(setId);
        return ResultInfo.success(setmeal);
    }
    //修改套餐内容
    @CacheEvict(value = "setmeal",allEntries = true)
    @PutMapping("/setmeal")
    public ResultInfo updateSetmeal(@RequestBody Setmeal setmeal){
        setmealService.updateSetmeal(setmeal);
        return ResultInfo.success();
    }
    //启停售
    @CacheEvict(value = "setmeal",allEntries = true)
    @PostMapping("/setmeal/status/{status}")
    public ResultInfo updateStatus(@PathVariable("status") Integer status,
                                   @RequestParam List<Long> ids){
        setmealService.updateStatus(status,ids);
        return ResultInfo.success();
    }

    //套餐删除
    @CacheEvict(value = "setmeal",allEntries = true)
    @DeleteMapping("/setmeal")
    public ResultInfo deleteSetmeal(@RequestParam List<Long> ids){
        setmealService.deleteSetmeal(ids);
        return ResultInfo.success();
    }
}

